﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MathLib {
  public  class TestMatrix {
        private int columnCount;
        private double sigma = 0.05;
        private Matrix[] populations;

        public TestMatrix(int columnCount) {
            this.columnCount = columnCount;
        }

        public Matrix GenerateA() {
            Matrix A = new Matrix(columnCount, columnCount);
            Matrix f = GenerateFertilityRate();
            Matrix m = GenerateMortalityRate();

            for (int j = 0; j < columnCount; j++)
                A[0, j] = f[j, 0];

            for (int i = 1; i < columnCount; i++)
                 A[i, i - 1] = m[i - 1,0];
            return A;

        }



        public Matrix[] GeneratePopulation() {
     
            Matrix[] P = new Matrix[3];
            for (int i = 1; i < 3; i++)
                P[i] = new Matrix(columnCount, 1);

            P[0] = GenerateInit();
            for (int i = 0;i < 2; i ++){
                Matrix A = GenerateA();
                Matrix U = GenerateMigrationRate();
                P[i + 1] = A * P[i] + U;
            }

            return P;
        }


        public Matrix GenerateInit() {
            Gauss g = new Gauss();
            Matrix v = new Matrix(columnCount, 1);
            if (columnCount == 2) {
                v[0, 0] = g.Next(25589, 12);
                v[1, 0] = g.Next(26634, 15);
            } else if (columnCount == 3) {
                v[0, 0] = g.Next(12300, 15);
                v[1, 0] = g.Next(25550, 15);
                v[2, 0] = g.Next(12200, 12);
            }
            return v;

        }

        public Matrix GenerateMigrationRate() {
            Gauss g = new Gauss();
            Matrix v = new Matrix(columnCount, 1);
            if (columnCount == 2) {
                v[0, 0] = g.Next(589, 12);
                v[1, 0] = g.Next(634, 15);
            } else if (columnCount == 3) {
                v[0, 0] = g.Next(300, 15);
                v[1, 0] = g.Next(550, 15);
                v[2, 0] = g.Next(200, 12);
            }
            return v;
      
        }

        private Matrix GenerateFertilityRate() {
            Gauss g = new Gauss();
            Matrix v = new Matrix(columnCount, 1);
            if (columnCount == 2) {
                v[0, 0] = g.Next(0.07, sigma);
                v[1, 0] = g.Next(0.8, sigma);
            } else if (columnCount == 3) {
                v[0, 0] = g.Next(0.02, 0.00015);
                v[1, 0] = g.Next(0.15, 0.00015 );
                v[2, 0] = g.Next(0.3, 0.00012);
            }
            return v;
        }

        private Matrix GenerateMortalityRate() {
            Gauss g = new Gauss();
            Matrix v = new Matrix(columnCount - 1, 1);
            if (columnCount == 2) {
                v[0, 0] = g.Next(0.89, sigma);
  
            }
            if (columnCount == 3) {
                v[0, 0] = g.Next(0.9, 0.015);
                v[1, 0] = g.Next(0.89, 0.015);

            }
            return v;
        }


    }
    
 
}
